#ifndef hash_h
#define hash_h
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

struct Node{
    string Word;
    int number;
    Node* next;
};

class HashTable {
private:
    Node* node[26];
public:
    HashTable(){
        for (int i = 0; i < 26; ++i)
            node[i] = NULL;
    }
    int geti(string s){
        char a = s[0];
        if(a >= 'A' && a <= 'Z')
            return a - 65;
        else if(a >= 'a' && a <= 'z')
            return a - 97;
        else return -1;
    }
    Node* search(string s){
        Node *p;
        int index;
        index = geti(s);
        for(p=node[index]; p; p=p->next){
            if(!strcmp(s.c_str(),p->Word.c_str()))
                return p;
        }
        return p;
    }
    bool insert(string key, int value){
        int index;
        Node *p;
        if(!(p = search(key))){
            index = geti(key);
            p = (Node*)malloc(sizeof(Node));
            if(!p) return false;
            p->Word = key;
            p->next = node[index];
            node[index] = p;
        }
        p->number = value;
        return true;
    }
    void display(){
        Node* temp;
        for(int i = 0; i < 26; ++i)
        {
            if(node[i])
                for (temp=node[i]; temp; temp=temp->next)
                    printf("%-20s %-5d\n", temp->Word.c_str(), temp->number);
        }
    }
    void printmax20(){
        Node* max;
        Node* temp;
        int j = 0;
        printf("\nThe 20 largest frequency words \n");
        printf("---------------------------------------------------\n");
        while(j++<20){
            max = node[0];
            for(int i = 0; i < 26; ++i){
                if(node[i])
                    for (temp=node[i]; temp->next; temp=temp->next)
                        if(max->number < temp->next->number)
                            max = temp->next;
            }
            printf("%-20s %-5d\n", max->Word.c_str(), max->number);
            max->number = 0;
        }
    }
};

